package mysql

import (
	"fmt"
	"gorm.io/gorm"
)

// Condition 结构体用于存储操作符和值
type Condition struct {
	Field    string      // 字段名
	Operator string      // 操作符
	Value    interface{} // 值
}

// ApplyQueryConditions 函数用于处理非等于查询条件
func ApplyQueryConditions(query *gorm.DB, conditions []Condition) *gorm.DB {
	for _, cond := range conditions {
		switch cond.Operator {
		case "=":
			query = query.Where(fmt.Sprintf("%s = ?", cond.Field), cond.Value)
		case "!=":
			query = query.Where(fmt.Sprintf("%s !=?", cond.Field), cond.Value)
		case "like":
			query = query.Where(fmt.Sprintf("%s LIKE ?", cond.Field), "%"+cond.Value.(string)+"%")
		case "in":
			query = query.Where(fmt.Sprintf("%s IN ?", cond.Field), cond.Value)
		case ">":
			query = query.Where(fmt.Sprintf("%s > ?", cond.Field), cond.Value)
		case "<":
			query = query.Where(fmt.Sprintf("%s < ?", cond.Field), cond.Value)
		case ">=":
			query = query.Where(fmt.Sprintf("%s >= ?", cond.Field), cond.Value)
		case "<=":
			query = query.Where(fmt.Sprintf("%s <= ?", cond.Field), cond.Value)
		default:
			// 可以根据需要添加更多操作符
		}
	}
	return query
}
